<?php
$user = $_SESSION["adminLoggedUserName"];

$title = trim($_POST["title"]);
$alias = Utils::generateAlias($title);
$visible = (isset($_POST["visible"]) && $_POST["visible"] == "on") ? 1 : 0;
$introduction = trim($_POST["introduction"]);
$description = trim($_POST["description"]);

switch ($_POST["operation"]) {
	// Add
	case "add":
		$operationName = "Inserción";
		$sql = "INSERT INTO blogEntries (
					created,
					createdBy,
					title,
					alias,
					visible,
					introduction,
					description
				) VALUES (
					NOW(),
					'$user',
					'".$db->escapeString(utf8_decode($title))."',
					'$alias',
					'$visible',
					'".$db->escapeString(utf8_decode($introduction))."',
					'".$db->escapeString(utf8_decode($description))."'
				)";
		break;
	// Edit
	case "edit":
		$id = $_POST["id"];
		$operationName = "Modificación";
		$sql = "UPDATE blogEntries SET
					modified = NOW(),
					modifiedBy = '$user',
					title = '".$db->escapeString(utf8_decode($title))."',
					alias = '$alias',
					visible = '$visible',
					introduction = '".$db->escapeString(utf8_decode($introduction))."',
					description = '".$db->escapeString(utf8_decode($description))."'
				WHERE id = '$id'";
		break;
	// Default
	default:
		echo "ko|No se ha especificado ninguna operación.";
		exit;
}

if (!$db->runQuery($sql)) {
	echo "ko|Error de MySQL: ".$db->getErrorDescription();
	exit;
}

echo "ok|".$operationName." correcta.";
exit;
?>
